#include "cuda_inc.h"
#include "maskGen.c"

/**
 * test
 * @author fred
 */
int main(int argc, char **argv) {
  //cuda_get_cuda_devices();
  unsigned char empreinte_md5[16] = {0x49,0x0e,0xb0,0x3e,0x31,0x73,0xf6,0xd7,0xd5,0x10,0x63,0x5d,0xd9,0x53,0xa2,0x44}; //empreinte MD5 de "a"
  unsigned char empreinte_sha1[20] = {0x34,0xAA,0x97,0x3C,0xD4,0xC4,0xDA,0xA4,0xF6,0x1E,0xEB,0x2B,0xDB,0xAD,0x27,0x31,0x65,0x34,0x01,0x6F}; //empreinte SHA1 de "a"
  unsigned char res[16];
  unsigned long long number_of_elements = 10, lastJobSize;
  int element_size = 16;
  int ret = -1;
  int i = 0, k=0, nbPacket, pwMin=6,pwMax=6;

  int mask[15] = {0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1};  
  unsigned char toto[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  unsigned char toto2[16] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  unsigned char toto3[16] = {'a', 'b', '@', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  unsigned char *to_hash = (unsigned char *)malloc(number_of_elements*element_size*sizeof(unsigned char));
  
  
  
  int hexDigitToInt(char hexDigit)
  {
	  switch(hexDigit)
	  {
		  case '0': return 0;
		  case '1': return 1;
		  case '2': return 2;
		  case '3': return 3;
		  case '4': return 4;
		  case '5': return 5;
		  case '6': return 6;
		  case '7': return 7;
		  case '8': return 8;
		  case '9': return 9;
		  case 'a': return 10;
		  case 'b': return 11;
		  case 'c': return 12;
		  case 'd': return 13;
		  case 'e': return 14;
		  case 'f': return 15;
		  default : -1;
	  }
  }
  int pos=0;
  for(i = 0; i < 16; i++)
  {
	  int high = hexDigitToInt(argv[1][pos]);
	  int low = hexDigitToInt(argv[1][pos+1]);
	  empreinte_md5[i] = (high << 4)+low;
	  pos += 2;
  }


  /*
  for (i=0; i<number_of_elements; i++) {
    memcpy(to_hash+16*i, toto3, 16*sizeof(unsigned char));
  }

  memcpy(to_hash+(16*(1)), toto2, 16*sizeof(unsigned char));
  */
  printf("=SEARCH MD5=\n");
  //ret = find_md5_checksum(to_hash, number_of_elements, empreinte_md5);
  
  number_of_elements = nbElement(mask);
  nbPacket = number_of_elements/JOB_SIZE;
  lastJobSize = number_of_elements%JOB_SIZE;

  if(number_of_elements < JOB_SIZE){
	  chunk(pwMin, pwMax, mask,0,0);
	  ret = find_md5_checksum(memPartagee, JOB_SIZE, empreinte_md5);
  }else{

	  for(i=0; i<nbPacket; i++){
		  chunk(pwMin,pwMax,mask,i*JOB_SIZE,(i+1)*JOB_SIZE);
		  fprintf(stderr,"Packet %i / %i\n",i+1,nbPacket);
		  ret = find_md5_checksum(memPartagee, JOB_SIZE, empreinte_md5);
		  if (ret == -1){
			  printf("Erreur\n");
			  return 0;
		  }
		  else if (ret == -2)
			  printf("Not found\n");
		  else{
			  indiceToword(res,mask,(ret+i*JOB_SIZE));
			  printf("Found  %s\n", res);
			  return 0;
		  }
	  }
  }




  printf("=SEARCH SHA1=\n");
  //ret = find_SHA1_checksum(memPartagee, number_of_elements, empreinte_md5);
  if (ret == -1)
    printf("Erreur\n");
  else if (ret == -2)
    printf("Not found\n");
  else
    printf("Found at indice %d\n", ret);
}
